package com.sky.controller.admin;


import com.sky.result.Result;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/admin/common")
@Slf4j
public class CommonController {

    @PostMapping("/upload")
    @ApiOperation("文件上传")
    public Result<String> upload(@RequestParam("file") MultipartFile image) throws IOException {
        log.info("上传文件{}", image);

        String originalFilename = image.getOriginalFilename();
        int index = originalFilename.lastIndexOf(".");
        String suffix = originalFilename.substring(index);
        String newFileName = UUID.randomUUID().toString()+suffix;

        log.info("新文件名{}", newFileName);
        image.transferTo(new File("D:\\javaweb\\cnagqing-project\\sky-take-out\\sky-server\\src\\main\\java\\com\\sky\\image\\"+newFileName));
        String url = "D:\\javaweb\\cnagqing-project\\sky-take-out\\sky-server\\src\\main\\java\\com\\sky\\image\\"+newFileName;
        return Result.success(url);

    };
}
